package edu.hebeu.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.hebeu.R;
import edu.hebeu.adapters.OrderDetailAdapter;
import edu.hebeu.entity.OrderDetailResponse;
import edu.hebeu.network.API;
import edu.hebeu.network.APIConfig;
import edu.hebeu.network.MyCallback;

public class OrderDetailActivity extends BaseActivity {

    private TextView tv_order_detail_title;
    private RecyclerView recycler_view;

    private Bundle order_data;
    private String order_code; // 要展示详情的订单号

    private List<OrderDetailResponse.OrderDetail> data; // 数据

    private OrderDetailAdapter adapter; // 适配器

    @Override
    protected int getLayoutId() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void initViews() {
        tv_order_detail_title = (TextView) findViewById(R.id.tv_order_detail_title);
        recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
        data = new ArrayList<>();
        getData();
    }

    private void show(List<OrderDetailResponse.OrderDetail> orderDetails) {
        // 创建布局管理器对象
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置水平
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        // 设置正向显示
        layoutManager.setReverseLayout(false);
        recycler_view.setLayoutManager(layoutManager); // 设置布局管理器

        // 创建适配器对象实例
        adapter = new OrderDetailAdapter(orderDetails);
        recycler_view.setAdapter(adapter);

        tv_order_detail_title.setText("订单--" + order_code + "--明细");
    }

    private void getData() {
        Intent intent = getIntent();
        order_data = intent.getBundleExtra("order_data");
        order_code = order_data.getString("code");

        Map<String, Object> params = new HashMap<>();
        params.put("order_code", order_code);
        requestOrderDetail(params);
    }

    private void requestOrderDetail(Map<String, Object> params) {
        String token = (String) getSPFile("token", "");
        API.requestConfig(APIConfig.URL.ORDER_DETAIL_URL, params, token)
            .postRequest(new MyCallback() {
                @Override
                public void onSuccess(String res) {
                    final OrderDetailResponse response = (OrderDetailResponse) deSerialize(res, OrderDetailResponse.class);
                    if (response == null) {
                        jumpActivity(LoginActivity.class, true);
                        showToast("身份验证失败，请重新登录", 0, true);
                    } else if (response.getCode() == 200) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                data.clear();
                                data.addAll(response.getOrderDetail());
                                show(data);
                            }
                        });
                    }

                }

                @Override
                public void onFailure(Exception e) {

                }
            }, true);

    }

}
